//
//  LoginViewController.m
//  DLTravels
//
//  Created by mac1012 on 2016/12/13.
//  Copyright © 2016年 DianlE. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginTextFiledCell.h"
#import "RegisteViewController.h"
#import "PasswordViewController.h"
#import "RootTabBarViewController.h"
#import "LoginModel.h"

#import <ReactiveCocoa/ReactiveCocoa.h>

@interface LoginViewController ()<UITableViewDataSource,UITableViewDelegate>{

    LoginModel *_loginModel;

}


@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    _myTableView = ({
        TPKeyboardAvoidingTableView *tableView = [[TPKeyboardAvoidingTableView alloc] initWithFrame:self.view.bounds];
        [tableView registerClass:[LoginTextFiledCell class] forCellReuseIdentifier:KCellIdentifier_LoginTextFileCell];
  
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView;
    });
    
    _loginModel = [[LoginModel alloc] init];
    
    _myTableView.tableHeaderView = [self customHeaderView];
    _myTableView.tableFooterView = [self customFooterView];
    [self.view addSubview:_myTableView];
    [self configButtomView];
}

- (UIView *)customHeaderView{
    
    CGFloat headHeight;
    if (kDevice_Is_iPhone4 || kDevice_Is_iPhone5) {
        headHeight = 80;
    }else if(kDevice_Is_iPhone6){
        headHeight = 90;
    }else if (kDevice_Is_iPhone6Plus){
        headHeight = 100;
    }else
        headHeight = 110;
    
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height/3)];
    return headView;
}

- (UIView *)customFooterView{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 150)];
    
    UIButton *loginBtn = [UIButton new];
    [view addSubview:loginBtn];
    
    NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithString:@"登录" attributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont systemFontOfSize:12.0],NSFontAttributeName,[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
   
    [loginBtn setAttributedTitle:attributedStr forState:UIControlStateNormal];
    [loginBtn setBackgroundColor:[UIColor colorWithHexString:@"4990E2"]];
    loginBtn.layer.masksToBounds = YES;
    loginBtn.layer.cornerRadius = 2.5;
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(70);
        make.right.mas_equalTo(-70);
        make.top.mas_equalTo(15);
        make.height.mas_equalTo(30);
    }];
    

    UIButton *resetPasswdBtn = [UIButton new];
    [view addSubview:resetPasswdBtn];
    

    NSAttributedString *attributedStrReset = [[NSAttributedString alloc] initWithString:@"忘记密码" attributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont systemFontOfSize:12.0],NSFontAttributeName,[UIColor colorWithHexString:@"9B9B9B"],NSForegroundColorAttributeName,nil]];
    [resetPasswdBtn setAttributedTitle:attributedStrReset forState:UIControlStateNormal];
    [resetPasswdBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(loginBtn);
        make.width.mas_equalTo(60);
        make.bottom.equalTo(loginBtn).offset(45);
        
    }];
    @weakify(self)
    loginBtn.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
       
   
        
//        [[Network_ApiManager shareNetworkApiManager] request_LoginTokenWithPath:[_loginModel urlPath] parameters:[_loginModel paramaters] block:^(id data, NSError *error) {
//           
//            
//        }];
        
        
        RootTabBarViewController *rootTabBarController = [[RootTabBarViewController alloc] init];
       
        kKeyWindow.rootViewController = rootTabBarController;
      
        NSLog(@"点击登录");
        return [RACSignal empty];
    }];
    
    resetPasswdBtn.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        
        [self.navigationController pushViewController:[[PasswordViewController alloc] init] animated:YES];
        return [RACSignal empty];
        
    }];
    
    
    
    return view;
}


- (void)configButtomView{

    UIButton *registBtn = [UIButton new];
    [self.view addSubview:registBtn];
    
    
    NSAttributedString *attributedStrReset = [[NSAttributedString alloc] initWithString:@"去注册" attributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont systemFontOfSize:12.0],NSFontAttributeName,[UIColor colorWithHexString:@"9B9B9B"],NSForegroundColorAttributeName,nil]];
    [registBtn setAttributedTitle:attributedStrReset forState:UIControlStateNormal];
    [registBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(60);
        make.bottom.equalTo(self.view).offset(-45);
        
    }];

    
    @weakify(self)
    registBtn.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        
        
        [self.navigationController pushViewController:[[RegisteViewController alloc] init] animated:YES];
        return [RACSignal empty];
        
    }];
    
    
    
}


#pragma mark UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LoginTextFiledCell *cell = [tableView dequeueReusableCellWithIdentifier:KCellIdentifier_LoginTextFileCell];
    
    if (indexPath.row == 0) {
        [cell setPlaceholder:@"账户" value:@""];
     
    }else if (indexPath.row == 1){
        [cell setPlaceholder:@"密码" value:@""];
      
    }
    
    @weakify(self)
    [cell.textFiled.rac_textSignal subscribeNext:^(id x) {
        @strongify(self)
        
       
        
        [self textFileDidChange:cell.textFiled];
    }];
    
    return cell;
}

- (void)textFileDidChange:(UITextField *)textFiled {
    NSLog(@"%@",textFiled.placeholder);
    
    if ([textFiled.placeholder isEqualToString:@"账户"]) {
        _loginModel.userName = textFiled.text;
    }else{
        _loginModel.userPw = textFiled.text;
    }
        
}


@end
